const axios = require('axios')
axios.defaults.withCredentials = true

const router = require('koa-router')()
const urlNavtive = 'https://fc-mp-d55e8f18-e485-4a28-a1b6-74998af468fc.next.bspapp.com'

router.prefix('/uniapi')

router.get('*', async function (ctx, next) {
  let url = ctx.request.url.replace('/uniapi', urlNavtive)
  let body = ctx.request.body
  let Cookie = ctx.request.header.cookie
  try {
    let res = await axios.default.request({
      method: 'get',
      url: url,
      data: body,
      responseType: 'json',
      headers: {
        Cookie
      },
      withCredentials: true
    })
    ctx.set('set-cookie', res.headers['set-cookie'])
    ctx.body = res.data
    return
  } catch (error) {
    ctx.body = error.response.data
    ctx.status = error.response.status
    return
  }
})

router.post('*', async function (ctx, next) {
  let url = ctx.request.url.replace('/uniapi', urlNavtive)
  let Cookie = ctx.request.header.cookie
  let body = ctx.request.body
  try {
    let res = await axios.default.request({
      method: 'post',
      url: url,
      data: body,
      responseType: 'json',
      headers: {
        Cookie
      },
      withCredentials: true
    })
    ctx.set('set-cookie', res.headers['set-cookie'])
    ctx.body = res.data
    return
  } catch (error) {
    ctx.body = error.response.data
    ctx.status = error.response.status
    return
  }
})
module.exports = router
